package br.com.amadeus.android.activities.course;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import br.com.amadeus.android.activities.course.goal.HomeGoalActivity;
import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.adapters.CourseInfoAdapter;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Config;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Goal;
import br.com.amadeus.android.entities.History;
import br.com.amadeus.android.entities.Resource;
import br.com.amadeus.android.historic.AmadeusDroidHistoric;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;
import br.com.amadeus.android.util.RESTFileContent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HomeCourseActivity extends Activity{
	
	protected static final String CATEGORIA = "curso";
	int course_id;
	int course_user_id;
	FacadeAmadeusDroid facade;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.amadeus_screen_course_info);
		//video de libras
		ImageButton librasBt = (ImageButton)findViewById(R.id.botaoLibras5);
        librasBt.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent it = new Intent("ABRIR_LIBRAS");
				it.putExtra("video_libras", "HomeCourse.wmv");
				startActivity(it);
				
			}
		});
		facade = FacadeAmadeusDroid.getInstance(this);

		initListAdapters();
		
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		initListAdapters();
	}

	private void initListAdapters() {
		
		Bundle gotBasket = getIntent().getExtras();
		course_id = gotBasket.getInt("course_id");
		
		Course course = facade.getCourse(course_id);
		TextView name = (TextView) findViewById(R.id.tvCourseNameInfo);
		name.setText(course.getName());
		
		ArrayList<Resource> resourceList = facade.getListResourcesByCourse(course.getId());
		course_user_id = facade.getCourseUser(course.getAmadeus_id());
		ArrayList<Goal> goalList = null;
		try {
			goalList = facade.getAllGoalByCourse(course_user_id);
		} catch (Exception e) {
			Log.e(CATEGORIA, e.getMessage(),e);
		}
		
		GridView gv = (GridView) findViewById(R.id.gvCourseInfo);
		gv.setAdapter(new CourseInfoAdapter(this, resourceList, goalList));
		
		gv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View v, int posicao,
					long id) {
				switch (posicao) {
				case 0:
					Intent it1 = new Intent("HOME_GOAL");
					Bundle goalBasket = new Bundle();
					goalBasket.putInt("course_id", course_id);
					it1.putExtras(goalBasket);
					startActivity(it1);
					break;
				case 1:
					//Intent itTask = new Intent("FORMTASK");
					Intent itTask = new Intent("HOME_TASK");
					Bundle taskBasket = new Bundle();
					taskBasket.putInt("course_id", course_id);
					itTask.putExtras(taskBasket);
					startActivity(itTask);
					// Salvar uma Tarefa e enviar para o WS.
					/*Config config = FacadeAmadeusDroid.getInstance().getConfig();
					AmadeusDroidHistoric historic = new AmadeusDroidHistoric();
					historic.setActionValue("Ler");
					historic.setCourseName("IHC");
					historic.setCreationDate(new Date());
					historic.setResourceName("Apostila Interfaces Humano-Computador");
					historic.setUserName(config.getUser_name());
					
					String url = config.getAmadeus_url() + ConstantsAmadeusDroid.HOST_AMADEUS_WEB_SOCIAL_HISTORY;
					RESTFileContent rfc = new RESTFileContent();
					try {
						rfc.postRESTFileContent(url, historic);
					} catch (ClientProtocolException e) {
						Log.e(CATEGORIA, e.getMessage(), e);
					} catch (IOException e) {
						Log.e(CATEGORIA, e.getMessage(), e);
					}*/
					
					break;
				case 2:
					Intent it = new Intent("HOME_RESOURCE");
					Bundle basket = new Bundle();
					basket.putInt("course_id", course_id);
					it.putExtras(basket);
					startActivity(it);
					break;
				}
				
			}
		});
		
	}


}
